package com.yksj.healthtalk.ui.messageleave;

import java.util.ArrayList;
import java.util.List;

import org.handmark.pulltorefresh.library.PullToRefreshBase;
import org.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import org.handmark.pulltorefresh.library.PullToRefreshListView;
import org.json.JSONObject;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.yksj.healthtalk.adapter.DoctorMessageAdapter;
import com.yksj.healthtalk.comm.BaseFragmentActivity;
import com.yksj.healthtalk.comm.SingleBtnFragmentDialog;
import com.yksj.healthtalk.entity.LeaveMessage;
import com.yksj.healthtalk.net.http.AsyncHttpResponseHandler;
import com.yksj.healthtalk.net.http.HttpRestClient;
import com.yksj.healthtalk.net.http.ObjectHttpResponseHandler;
import com.yksj.healthtalk.ui.R;
import com.yksj.healthtalk.utils.ToastUtil;
/**
 * 公告管理
 * @author Administrator
 *
 */
public class DoctorMessageBoardManager extends BaseFragmentActivity implements OnClickListener ,OnRefreshListener<ListView>{
	private PullToRefreshListView mPullToRefreshListView;
	private ListView mListView;
	private DoctorMessageAdapter adapter;
	private int loadNum=0;
	private List<LeaveMessage> list=new ArrayList<LeaveMessage>();
	private boolean isUpdate=false;
	private String customerid = null;
	private Button mDelete;
	private Button mDeleteAll;
	
	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		setContentView(R.layout.doctor_message_manager);
		customerid = getIntent().getStringExtra("customerid");
		initTitle();
		initView();
	}

	private void initView() {
		titleLeftBtn.setOnClickListener(this);
		titleTextV.setText("公告管理");
		mDelete=(Button) findViewById(R.id.delete);
		mDelete.setOnClickListener(this);
		mDeleteAll=(Button) findViewById(R.id.delete_all);
		mDeleteAll.setOnClickListener(this);
		mPullToRefreshListView = (PullToRefreshListView)findViewById(R.id.hall);
		mListView = mPullToRefreshListView.getRefreshableView();
		mPullToRefreshListView.setOnRefreshListener(this);
		adapter = new DoctorMessageAdapter(list,this,2);
		mListView.setAdapter(adapter);
		initData();
	}
	

	@Override
	public void onRefresh(PullToRefreshBase<ListView> refreshView) {
		loadNum++;
		initData();
	}

	private void initData() {
		HttpRestClient.doHttpLookUserLeaveMessage(customerid,"",loadNum+"",
				new ObjectHttpResponseHandler(this) {
			@Override
			public Object onParseResponse(String content) {
				if(customerid != null){
					try{
						JSONArray array = JSON.parseArray(content);
						com.alibaba.fastjson.JSONObject jsonObject = new com.alibaba.fastjson.JSONObject();
						jsonObject.put("message", array);
						jsonObject.put("notice", new JSONObject());
						content = jsonObject.toJSONString();
					}catch (Exception e) {
						return null;
					}
				}
				return adapter.onparseJson(content,loadNum);
			}
			@Override
			public void onSuccess(Object response) {
				if(response == null)return;
				adapter.mList.addAll((List<LeaveMessage>)response);
				adapter.notifyDataSetChanged();
				//如果没有数据,隐藏删除以及全部删除
				
				if(adapter.mList.size()==0){
					mDelete.setVisibility(View.GONE);
					mDeleteAll.setVisibility(View.GONE);
				}
			}
			@Override
			public void onFinish() {
				mPullToRefreshListView.onRefreshComplete();
				super.onFinish();
			}
		});
	}
	private void deleteMessage(String str){
		HttpRestClient.doHttpdeleteDoctorMessage(str,new AsyncHttpResponseHandler(this){
			@Override
			public void onSuccess(String content) {
				super.onSuccess(content);
				try {//{"info":"操作成功","code":"0"}
					JSONObject object=new JSONObject(content);
					ToastUtil.showBasicShortToast(getApplicationContext(), object.getString("info"));
					if("0".equals(object.getString("code"))){
						isUpdate=true;
						adapter.mList.clear();
						loadNum=0;
						initData();
						SingleBtnFragmentDialog.showDefault(getSupportFragmentManager(),  "操作成功");
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	} 
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.delete:
			StringBuffer buffer=new StringBuffer();
			for (int i = 0; i <adapter.mList.size(); i++) {
				LeaveMessage message = adapter.mList.get(i);
				if(message.isCheck()==2){
					if(buffer.length()==0){
						buffer.append(message.getMESSAGE_ID());
					}else{
						buffer.append(","+message.getMESSAGE_ID());
					}
				}
			}
			if(buffer.toString().length()==0)return ;
			deleteMessage(buffer.toString());
			break;
		case R.id.delete_all:
			deleteMessage("ALL");
			break;
		case R.id.title_back:
			if(isUpdate){
				setResult(RESULT_OK, getIntent());
			}
			
			finish();
			overridePendingTransition(R.anim.back_enter,R.anim.back_exit);
			break;
		}
		
	}
}
